require(["config"],function(){
    require(["jquery","template","cookie","load"],function($,template){
        function shoppingCart(){
            this.init();
            $.cookie.json = true;
        };
        $.extend(shoppingCart.prototype,{
            init(){
                // 事件函数处理
                this.shoppingCartEventHandler();
                // 页面数据加载
                this.loadShoppingCartDate();
            },
            // 事件处理
            shoppingCartEventHandler(){
                //点击确定，保存默认地址
                $(".set-default-add").on("click",$.proxy(this.saveAddress,this));
                // 点击修改地址
                $(".modify").on("click",$.proxy(this.modifyAddress,this));
                // 点击新建地址
                $(".new-address").on("click",$.proxy(this.newAddress,this));
            },
            loadShoppingCartDate(){
                const _search = location.search.slice(1).split("&");
                const _id = _search.map(curr => {
                    return curr.split("=").pop();
                })
                if(_id[0] != 0){
                    const productId = _id[0];
                    $.getJSON("http://rap2api.taobao.org/app/mock/data/720657",data =>{
                        const _data = data.res_body.data;
                        _data.forEach((curr,index) => {
                            if(curr.id == productId){
                                const _dataHtml = template("shoppingArr",{shoppingArray:curr.detaile_arr,num:_id[1]});
                                $(".shopping-product-list-box").html(_dataHtml);
                                const _settPro = curr.detaile_arr.de_pre * _id[1];
                                $(".heji").text("￥" + _settPro);
                                $(".payment-price").text("￥" + _settPro);
                            }
                        })
                    })   
                }else{
                    const _settCart = $.cookie("cart");
                    const CartData = template("shoppingCart",{shCarPro:_settCart});
                    $(".shopping-product-list-box").html(CartData);
                    const _cartdata = $(".product-sett").text().slice(1).split("￥");
                    console.log(_cartdata)
                    const _sett = _cartdata.reduce((curr,item)=>{
                        return curr += Number(item);
                    },0);
                    $(".heji").text("￥" + _sett.toFixed(2));
                    $(".payment-price").text("￥" + _sett.toFixed(2));
                }

            },
            // 默认地址事件函数处理
            saveAddress(){
                const _province = $("#province").val(),
                _city = $("#city").val(),
                _area = $("#area").val(),
                _des = $("#delute").val(),
                _address = _province + "省 " + _city + "市 " + _area + "区 " + _des;
                $("#writeAddress").slideUp(300);
                $("#defaultAddress").show(300);
                $(".consignee").text($(".name").val());
                $(".contact-number").text($(".phone-input").val());
                $(".receiving-address").text(_address);
            },
            // 修改地址事件函数处理
            modifyAddress(){
                $("#defaultAddress").hide(300);
                $("#writeAddress").slideDown(300);
                $(".name").val($(".consignee").text());
                $(".phone-input").val($(".contact-number").text());
            },
            // 新建地址的事件处理函数
            newAddress(){
                $(".name").val("");
                $(".phone-input").val("");
                $(".delute").val("");
                $("#defaultAddress").hide(300);
                $("#writeAddress").slideDown(300);
            }
        });
        new shoppingCart();
    })
})